﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.WeChat;
using YunSpace.Domain.IRepository.WeChat;
using YunSpace.Repository.WeChat;
using System.Collections.Generic;
using System.Linq;
using YunSpace.Domain.ViewModel.WeChat;
using YunSpace.Data;
using System.Data.Common;
using MySql.Data.MySqlClient;
#endregion

namespace YunSpace.Application.WeChat
{
    public class CommentApp
    {
        private ICommentRepository service = new CommentRepository();

        public List<CommentEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<CommentEntity>();
            var queryParam = queryJson.ToJObject();
            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }

        public CommentEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void SubmitForm(CommentEntity commentEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                commentEntity.Modify(keyValue);
                service.Update(commentEntity);
            }
            else
            {
                commentEntity.Create();
                // commentEntity.F_Category = 2;
                service.Insert(commentEntity);
            }
        }

        /// <summary>
        /// 根据订单号获取
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public CommentEntity GetCommentByOrderId(string orderId)
        {
            return service.FindEntity(x => x.F_OrderId == orderId);
        }

        /// <summary>
        /// 根据场地id获取评论
        /// </summary>
        /// <param name="siteId"></param>
        /// <returns></returns>
        public List<CommentEntity> GetCommentsBySiteId(string siteId)
        {
            return service.IQueryable(x => x.F_SiteId == siteId).ToList();
        }



        /// <summary>
        /// 根据场地id统计评论
        /// </summary>
        /// <param name="siteId"></param>
        /// <returns></returns>
        public WeCharCommentModel GetCommentBySiteId(string siteId)
        {
            using (var db = new RepositoryBase())
            {
                var strSql = @"select sum(F_SiteLike)F_SiteLike,F_SiteId as F_Id,format(sum(F_SiteLike)/count(F_SiteId),0) star from WeChat_Comment wc where wc.F_SiteId=@fId  group by wc.F_SiteId ";
                DbParameter[] parameter =
                {
                    new MySqlParameter("@fId", siteId)
                };
                return db.FindList<WeCharCommentModel>(strSql, parameter).FirstOrDefault();
            }
        }
        /// <summary>
        /// 根据空间id统计评论
        /// </summary>
        /// <param name="spaceId"></param>
        /// <returns></returns>
        public WeCharCommentModel GetCommentBySpaceId(string spaceId)
        {
            using (var db = new RepositoryBase())
            {
                var strSql = @"select sum(F_SiteLike)F_SiteLike,F_SpaceId as F_Id,format(sum(F_SiteLike)/count(F_SpaceId),0) star from WeChat_Comment wc where wc.F_SpaceId=@fId  group by wc.F_SpaceId ";
                DbParameter[] parameter =
                {
                    new MySqlParameter("@fId", spaceId)
                };
                return db.FindList<WeCharCommentModel>(strSql, parameter).FirstOrDefault();
            }
        }
    }
}

